package com.nlabs.slickgwt.client;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class ImageButton extends FocusPanel {
 
	public enum ImageAlign {LEFT, RIGHT};
	
	protected Image m_image = new Image();
	protected HTML m_button_label = new HTML(); 
	protected HorizontalPanel m_hpane = new HorizontalPanel();
	private MouseHandler mh = new MouseHandler(); 
 
	
	
	public ImageButton() {
		
		layout(); 
		
	}
	
	public ImageButton(String url, String text) {
		
		layout();
		m_image.setUrl(url);
		m_button_label.setHTML(text);

	}
	
	private void layout() {
		
		m_button_label.setStylePrimaryName("slick-image-button-label");
		setStylePrimaryName("slick-image-button");
		m_hpane.setSize("100%", "100%");
		m_hpane.setSpacing(2);
		alignImage(ImageAlign.LEFT);
		setWidget(m_hpane);
		addMouseOutHandler(mh);
		addMouseOverHandler(mh);
		addMouseDownHandler(mh);
		addMouseUpHandler(mh);
	 
	}
	
	private void alignImage(ImageAlign im){
		
		m_hpane.clear(); 
		m_hpane.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		if(im == ImageAlign.LEFT){ 
			m_hpane.add(m_image);
			m_hpane.add(m_button_label); 
		}
		else{
		 
			m_hpane.add(m_button_label);
			m_hpane.add(m_image);
		 
		}
		
		m_hpane.setCellHorizontalAlignment(m_button_label, HasHorizontalAlignment.ALIGN_CENTER);
		
		
	}
	
	public void setImageAlign(ImageAlign im){
	
		alignImage(im);	
	}
	
	public void setText(String txt){
		
		m_button_label.setHTML(txt);
	}
	
	public void setImageUrl(String url){
		
		m_image.setUrl(url);
	}
	
	public void setImageUrlAndRect(String url, int left, int top, int width, int height){
		
		m_image.setUrlAndVisibleRect(url, left, top, width, height);
	}
	
	public void setPadding(int sp){
		
		m_hpane.setSpacing(sp);
	}
	
	
	private class MouseHandler implements MouseOverHandler, MouseOutHandler, MouseDownHandler, MouseUpHandler {
	
    	
    	public void onMouseOver(MouseOverEvent event) {
    		addStyleDependentName("hover");
		
    	}

    	public void onMouseOut(MouseOutEvent event) {
		 removeStyleDependentName("hover");
		
    	}

    	public void onMouseDown(MouseDownEvent event) {
		
    		event.preventDefault(); 
    		removeStyleDependentName("hover");
    		addStyleDependentName("active");
    		DOM.setCapture(getElement());
    	}

    	public void onMouseUp(MouseUpEvent event) {
		
    		removeStyleDependentName("active");
    		addStyleDependentName("hover");
    		DOM.releaseCapture(getElement());
    	}
    }
	
}
